package com.byzk.p2p.admin.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.byzk.p2p.admin.base.controller.AdminBaseController;
import com.byzk.p2p.admin.base.util.JsonMsg;
import com.byzk.p2p.admin.base.util.PageInfo;
import com.byzk.p2p.admin.base.util.RequestUtil;
import com.byzk.p2p.admin.web.dto.Article;
import com.byzk.p2p.admin.web.dto.ArticleType;
import com.byzk.p2p.admin.web.service.ArticleService;
import com.byzk.p2p.admin.web.vo.ArticleVo;


/**
 * 文章Controller
 * 
 * @author LiuJY
 *
 */
@Controller
@RequestMapping("/admin/article")
public class ArticleController extends AdminBaseController{
	@Autowired
	private ArticleService articleService;
	
	/**
	 * 分页查询文章列表
	 * @param model 视图对象
	 * @param index 当前页
	 * @param limit 每页显示条数
	 * @param articleType 文章类型
	 * @param title 文章标题
	 * @return
	 */
	@RequestMapping("/list")
	public String listArticle(Model model,
						@RequestParam(value = "index", required = false, defaultValue = "1") int index,
						@RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
						@RequestParam(value = "articleType", required = false) String articleType,
						@RequestParam(value = "title", required = false) String title) {
		//根据articleType获得typeId
		Integer typeId = articleService.getIdByType(articleType);
		Map<String, Object> queryMap = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(title)) {
			queryMap.put("title", title);
			model.addAttribute("title", title);
		}
		if (StringUtils.isNotBlank(articleType)) {
			queryMap.put("type", articleType);
			model.addAttribute("type", articleType);
		}
		model.addAttribute("articleTypes",articleService.articleTypeList());
		PageInfo<ArticleVo> pageInfo = articleService.articleList(queryMap,index, limit);
		model.addAttribute("pageInfo", pageInfo);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	/**
	 * 保存文章
	 * @param article 文章
	 * @return
	 */
	@RequestMapping(value="/saveArticle",method=RequestMethod.POST)
	public void saveArticle(@RequestBody Article article){
		try {
			if (StringUtils.isBlank(article.getTitle())) {
				printJSON(new JsonMsg(1, "标题不能为空"));
				return;
			}
			if (article.getTypeId() == null) {
				printJSON(new JsonMsg(2, "类目不能为空"));
				return;
			}
			if (StringUtils.isBlank(article.getAuthor())) {
				printJSON(new JsonMsg(3, "作者不能为空"));
				return;
			}
			if (StringUtils.isBlank(article.getTags())) {
				printJSON(new JsonMsg(4, "标签不能为空"));
				return;
			}
			if (article.getArticleOrder() == null) {
				printJSON(new JsonMsg(5, "排序号不能为空"));
				return;
			}
			if (StringUtils.isBlank(article.getContent())) {
				printJSON(new JsonMsg(6, "内容不能为空"));
				return;
			}
			if (article.getId() == null) {
				article.setHits(0);
				article.setStatus(0);//未发布
				article.setCreateIp(RequestUtil.getRemortIP(request));
				article.setCreateTime(new Date());
			}else{
				article.setStatus(1);//已发布
				article.setUpdateIp(RequestUtil.getRemortIP(request));
				article.setUpdateTime(new Date());
			}
			Integer i = articleService.saveOrUpdateArticle(article);
			if (i > 0) {
				printJSON(new JsonMsg(-1, "操作成功"));
			} else {
				printJSON(new JsonMsg(0, "操作失败"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 设置文章内容
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping("/settingArticle")
	public String settingArticle(Model model,@RequestParam(value="id", required = false, defaultValue = "0") Integer id){
		ArticleVo articleVo = articleService.getArticleById(id);
		List<ArticleType> articleTypes = articleService.articleTypeList();
		model.addAttribute("item", articleVo);
		model.addAttribute("articleTypes", articleTypes);
		return ftl(Thread.currentThread().getStackTrace());
	}
	
	/**
	 * 删除文章
	 * @param id 文章id
	 * @return
	 */
	@RequestMapping("/delArticle")
	public String delArticle(Model model,@RequestParam Integer id){
		articleService.deleteArticleById(id);
		return "redirect:/admin/article/list";
	}
	
}
